<template>
  <div class="artistAccount">
    <div class="nav" />
    <div class="artist-info">
      <!-- 歌手信息区域 -->
      <div class="artist-top">
        <!-- 头像 -->
        <div class="artist-avatar">
          <img class="img" :src="getartist.cover + '?param=200y200'" alt="">
        </div>
        <!-- 头像右侧信息 -->
        <div class="artist-info-detail">
          <div class="name">{{getartist.name}}</div>
          <div class="alias">{{getalias}}</div>
          <div class="btn-group">
            <el-button @click="artistsub()" v-show="!isSub" size="medium" icon="el-icon-folder-add" round>收藏</el-button>
            <el-button @click="artistsub()" v-show="isSub" size="medium" icon="el-icon-folder-checked" round>已收藏
            </el-button>
            <el-button @click="$router.push({ name:'userAccount',query:{ id:artistDetail.user.userId } })"
              v-if="artistDetail.user != undefined" size="medium" round icon="el-icon-user">个人主页</el-button>
          </div>
          <div class="info-group">
            <div class="musiccount">单曲数:{{artist.musicSize}}</div>
            <div class="albumcount">专辑数:{{artist.albumSize}}</div>
            <div class="mvcount">MV数:{{artist.mvSize}}</div>
          </div>
        </div>
      </div>
      <!-- 歌手专辑详情区域 -->
      <div class="artist-bottom">

        <el-tabs v-model="activeName">
          <el-tab-pane label="专辑" name="first">
            <div class="artist-album">
              <albumItem v-for="(item,index) of artistAlbum" :key="index+'album'" :album="item" />
            </div>
          </el-tab-pane>
          <el-tab-pane label="歌手详情" name="second">
            <div class="artist-Desc">
              <div class="title">{{getartist.name}}简介</div>
              <div class="content">{{getartist.briefDesc}}</div>
            </div>

          </el-tab-pane>
          <el-tab-pane label="相似歌手" name="third">
            <div class="simi-art">
              <simiartist v-for="(item,index) of simiartists" :artist="item" :key="index+'simi'" />
            </div>

          </el-tab-pane>
        </el-tabs>
      </div>

    </div>

  </div>
</template>
<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import {
  artistDetailAPI,
  artistAlbumAPI,
  simiartistAPI,
  artistAPI,
  artistsubAPI,
} from "@/api/artist";
import albumItem from "@/components/album-item/album-item";
import simiartist from "./components/similarArtist.vue";
export default {
  name: "artistAccount",
  //import引入的组件需要注入到对象中才能使用
  components: { albumItem, simiartist },
  props: {},
  data() {
    //这里存放数据
    return {
      // 从歌手详情api获得的歌手信息
      artist: {
        artist: undefined,
      },
      artistDetail: {},
      // 标签页
      activeName: "first",
      // 从歌手专辑api获得的歌手信息
      artist2: {
        artist: undefined,
      },
      artist3: {
        artist: undefined,
      },
      // 是否收藏
      isSub: false,
      artistAlbum: {},
      // 相似歌手
      simiartists: {},
      // 从获取歌手单曲获得歌手信息
    };
  },
  //计算属性 类似于data概念
  computed: {
    getartist() {
      if (this.artist.cover) {
        return this.artist;
      }
      return this.artist || "";
    },
    getartist2() {
      if (this.artist2.artist) {
        return this.artist2.artist;
      }
      return "";
    },

    getalias() {
      if (this.artist2.artist) {
        return this.artist2.artist.alias[0];
      }
      return "";
    },
  },
  //监控data中的数据变化
  watch: {
    // 监听路由参数
    $route: {
      handler: function () {
        // console.log(val.query.id);
        this.getartistDetail();
        this.getartistDetail2();
        this.getartistDetail3();
        this.simiartist();
      },
      // 深度观察监听
      deep: true,
    },
  },
  //方法集合
  methods: {
    async getartistDetail() {
      try {
        const { data } = await artistDetailAPI(this.$route.query.id);
        this.artistDetail = data.data;
        // console.log(data, "歌手详情");
        this.artist = data.data.artist;
        document.title = this.artist.name + "-歌手";
        // console.log(this.artistDetail.user);
      } catch (error) {
        console.log(error, "获取歌手信息失败");
      }
    },
    async getartistDetail2() {
      try {
        const artistAlbum = await artistAlbumAPI(this.$route.query.id);
        // console.log(artistAlbum, "歌手专辑");
        this.artist2 = artistAlbum.data;
        this.artistAlbum = artistAlbum.data.hotAlbums;
        // console.log(this.artistDetail.user);
      } catch (error) {
        console.log(error, "获取歌手专辑失败");
      }
    },

    async getartistDetail3() {
      try {
        const { data } = await artistAPI(this.$route.query.id);
        // console.log(data, "部分信息和热门单曲");
        this.artist3 = data;
        this.isSub = data.artist.followed;
      } catch (error) {
        console.log(error, "部分信息和热门单曲");
      }
    },
    // 获取相似歌手
    async simiartist() {
      try {
        const { data } = await simiartistAPI(this.$route.query.id);
        // console.log(data, "获取相似歌手");
        this.simiartists = data.artists;
      } catch (error) {
        console.log(error, "获取相似歌手失败");
      }
    },
    // 收藏取消收藏
    async artistsub() {
      try {
        // 1为收藏，其他为取消
        if (this.isSub) {
          const { data } = await artistsubAPI(this.$route.query.id, 0);
          console.log(data, "取消收藏");
          this.isSub = false;
        } else if (!this.isSub) {
          const { data } = await artistsubAPI(this.$route.query.id, 1);
          console.log(data, "收藏");
          this.isSub = true;
        }
      } catch (error) {
        console.log(error, "收藏失败");
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    console.log(this.$route.query.id);
    this.getartistDetail();
    this.getartistDetail2();
    this.getartistDetail3();
    this.simiartist();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.artistAccount {
  position: relative;
  left: 0;
  right: 0;
  top: 70px;
  width: 100%;
  min-width: 1200px;

  .nav {
    position: relative;
    height: 5px;
    background-color: #9e9e9e;
  }
  .artist-info {
    padding-top: 20px;
    width: 1200px;
    height: 250px;
    display: flex;
    margin: 0 auto;
    flex-wrap: wrap;
    .artist-top {
      width: 1200px;
      display: flex;
      flex-shrink: 0;
      .artist-avatar {
        .img {
          width: 200px;
          height: 200px;
          border-radius: 5px;
          border: 1px solid rgb(233, 232, 232);
        }
      }
      .artist-info-detail {
        margin-left: 20px;

        flex-shrink: 0;
        .name {
          font-size: 25px;
          font-weight: 600;
          color: rgb(44, 44, 44);
        }
        .alias {
          margin-top: 10px;
          color: rgb(44, 44, 44);
          font-size: 18px;
        }
        .btn-group {
          margin-top: 10px;
        }
        .info-group {
          margin-top: 20px;
          display: flex;
          cursor: default;
          .musiccount {
            color: rgb(44, 44, 44);
          }
          .albumcount {
            margin-left: 30px;
            color: rgb(44, 44, 44);
          }
          .mvcount {
            margin-left: 30px;
            color: rgb(44, 44, 44);
          }
        }
      }
    }
    .artist-bottom {
      width: 1200px;
      //为音乐播放器留位置
      padding-bottom: 80px;
      .artist-Desc {
        .title {
          color: #3d3d3d;
          font-weight: 600;
          line-height: 30px;
        }
        .content {
          color: #868686;
          width: 1200px;
          line-height: 30px;
          white-space: pre-wrap;
        }
      }
    }
  }
  .artist-album {
    display: flex;
    flex-wrap: wrap;
    //为音乐播放器留位置
    padding-bottom: 150px;
  }
  .simi-art {
    display: flex;
    flex-wrap: wrap;
    //为音乐播放器留位置
    padding-bottom: 150px;
  }
  /deep/ .el-tabs__nav-wrap::after {
    content: "";
    width: 0px;
  }
  /deep/ .is-active {
    font-size: 20px;
    font-weight: 800;
    color: #3d3d3d;
  }
  /deep/ .el-tabs__item {
    padding-left: 5px;
    padding-right: 5px;
    width: 130px;
    color: #3d3d3d;
    text-align: center;
  }
}
</style>